var api = require('./../../../config/api.js');
var app = getApp();
var util = require('./../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    man: true, // 是否为男性
    addressArray: [],
    addressIdArray: [],
    addressText: "",  // 地址内容
    addressId: 0,
    mobile: "",  // 手机号
    dorm: "",  // 宿舍号
    school: '',
    schoolId: 0,
    orderType: 'express',
    customAddressText: "",  // 地址内容
    toDo: 'express',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('page:add.js');
    console.log(options)
    this.setData({ orderType: options.orderType});
    if (options.do){
      this.setData({ toDo: options.do });
    }
    var that = this;

    that.setData({ 
      school: app.globalData.userInfo.addition.uSchool,
      schoolId: app.globalData.userInfo.addition.uCustomInfo.csId,
      man: app.globalData.userInfo.addition.uSex,
      name: app.globalData.userInfo.addition.uName,
      mobile: app.globalData.userInfo.addition.uMobile,
    });

    util.myRequest(api.RangeList + '/site/' + app.globalData.userInfo.addition.uCustomInfo.csId, false, function(data){
      that.setData({
        addressArray: data.data.nameList,
        addressIdArray: data.data.idList,
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.globalData.shareInfo;
  },

  // 选择性别男
  chooseMan: function () {
    this.setData({ man: true});
  },

  // 选择性别女
  chooseWoman: function () {
    this.setData({ man: false });
  },

  // // 加载下拉框第一个地址
  // loadDefaultAddress: function (e) {
  //   if (this.data.addressText == "") {
  //     this.setData({addressText : this.data.addressArray[0][0] + "/" + this.data.addressArray[1][0] + "/" + this.data.addressArray[2][0]});
  //   }
  // },

  // 地址下拉框触发
  addressChange: function (e){
    console.log('picker发送选择改变，携带值为', e.detail.value);
    var that = this;
    that.setData({
      addressText: that.data.addressArray[e.detail.value],
      addressId: that.data.addressIdArray[e.detail.value],
    });

  },

  // // 下拉框内容变动触发
  // addressColumnChange: function (e) {
  //   var that = this;
  //   console.log('修改的列为', e.detail.column, '，值为', e.detail.value); //修改的列为 1 ，值为 1
  //   var data = {
  //     addressArray: this.data.addressArray,
  //     addressIndex: this.data.addressIndex,
  //     addressIdArray: this.data.addressIdArray,
  //   };
  //   data.addressIndex[e.detail.column] = e.detail.value;
  //   // 第几列
  //   switch (e.detail.column) {
  //     case 0:

  //       break;
  //     case 1:
  //       var scId = data.addressIdArray[e.detail.column][e.detail.value];
  //       util.myRequest(api.RangeList + '/site/' + scId, false, function(data){
  //         data.addressArray[2] = data.data.nameList;
  //         data.addressIdArray[2] = data.data.idList;
  //         data.addressIndex[2] = 0;
  //         that.setData(data);
  //       });
  //       break;
  //   }
  //   data.addressText = data.addressArray[0][data.addressIndex[0]] + "/" + data.addressArray[1][data.addressIndex[1]] + "/" + data.addressArray[2][data.addressIndex[2]];
  //   this.setData(data);
  // },

  // 保存并跳转到选择地址
  chooseAddress: function() {

    var that = this;
    if (that.data.name == "") {
      wx.showToast({
        title: '请填写您的称呼',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (that.data.addressText == "" && that.data.orderType == 'express'){
      wx.showToast({
        title: '请填写地址信息',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (that.data.mobile == "") {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (that.data.dorm == "" && that.data.orderType == 'express') {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (that.data.customAddressText == "" && that.data.orderType == 'custom') {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    // 提交地址
    var postData = {
      token: app.globalData.userInfo.token,
      name: that.data.name,
      sex: that.data.man ? 1 : 0,
      mobile: that.data.mobile,
      school: that.data.school,
      schoolId: that.data.schoolId,
      site: that.data.addressText,
      siteId: that.data.addressId,
      address: that.data.dorm,
      customAddress: that.data.customAddressText
    };

    util.myRequest(api.ReceiveAddress, postData, function(data){
      wx.redirectTo({
        url: './../list/list?orderType=' + that.data.orderType + '&do=' + that.data.toDo
      })
    });
  },

  // 获取姓名
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  // 获取手机号
  mobileInput: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  // 获取宿舍号（详细地址）
  dormInput: function (e) {
    this.setData({
      dorm: e.detail.value
    })
  },

  // 自定义地址
  customInput: function (e) {
    this.setData({
      customAddressText: e.detail.value
    })
  },
})